package com.tian.core.redis;

import com.tian.core.utils.SerializeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 单机版redis操作
 * @author YangTao
 * @date 2025/4/18
 */
@Component
public class RedisPool implements JedisManager{

    private static Logger logger = LoggerFactory.getLogger(RedisPool.class);
//    @Autowired
    private JedisPool jedisPool;

    @Override
    public void set(String key, String value) {
        Jedis jedis = getJedis();
        try{
            jedis.set(key,value);
        }catch (Exception ex){
            logger.error("redis set error",ex);
        }finally {
            close(jedis);
        }
    }

    @Override
    public void set(String key, String value, Integer seconds) {
        Jedis jedis = getJedis();
        try{
            jedis.set(key,value);
            jedis.expire(key,seconds);
        }catch (Exception ex){
            logger.error("redis set error",ex);
        }finally {
            close(jedis);
        }
    }

    @Override
    public String get(String key) {
        Jedis jedis = getJedis();
        String result = null;
        try{
            result =  jedis.get(key);
        }catch (Exception ex){
            logger.error("redis get error",ex);
        }finally {
            close(jedis);
        }
        return result;
    }

    @Override
    public void del(String key) {
        Jedis jedis = getJedis();
        try{
           jedis.del(key);
        }catch (Exception ex){
            logger.error("redis delete error",ex);
        }finally {
            close(jedis);
        }
    }

    @Override
    public void expire(String key, Integer seconds) {
        Jedis jedis = getJedis();
        try{
            jedis.expire(key,seconds);
        }catch (Exception ex){
            logger.error("redis expire error",ex);
        }finally {
            close(jedis);
        }
    }

    @Override
    public Long incr(String key,Long value) {
        Jedis jedis = getJedis();
        Long result = null;
        try{
            result = jedis.incrBy(key,value);
        }catch (Exception ex){
            logger.error("redis incr error",ex);
        }finally {
            close(jedis);
        }
        return result;
    }

    @Override
    public void setObject(String key, Object object) {
        Jedis jedis = getJedis();
        try{
            jedis.set(key, SerializeUtil.serializationObject(object));
        }catch (Exception ex){
            logger.error("redis setObject error",ex);
        }finally {
            close(jedis);
        }
    }

    @Override
    public void setObject(String key, Object object, Integer seconds) {
        Jedis jedis = getJedis();
        try{
            jedis.set(key, SerializeUtil.serializationObject(object));
            jedis.expire(key.getBytes(),seconds);
        }catch (Exception ex){
            logger.error("redis setObject error",ex);
        }finally {
            close(jedis);
        }
    }

    @Override
    public Object getObject(String key) {
        //查询
        Jedis jedis = this.getJedis();
        try{
            String result = jedis.get(key);
            //如果查询没有为空
            if(null == result){
                return null;
            }
            //查询到了，反序列化
           return SerializeUtil.deserializationObject(result);
        }catch (Exception ex){
            logger.error("redis setObject error",ex);
        }finally {
            close(jedis);
        }
        return null;
    }

    /**
     * 获取Jedis连接
     *
     * @return Jedis连接
     */
    private Jedis getJedis() {
        return this.jedisPool.getResource();
    }

    /**
     * 释放Jedis连接,返还到连接池
     *
     * @param jedis
     *            jedis连接
     */
    private void close(Jedis jedis) {
        jedis.close();
    }
}
